<template>
	<view class="content" :style="{background: `url(${imgPath}storage/home/home_bg.png)`}">
		<view class="box">
			<view class="address" @click="goAddress">
				<view class="haveAddress" v-if="addressForm.address">
					<view class="left">
						<u-icon name="map" size="20"></u-icon>
					</view>
					<view class="right">
						<view class="top">
							<view class="name">
								{{addressForm.name}}
							</view>
							<view class="phone">
								{{addressForm.mobile}}
							</view>
						</view>
						<view class="bottom">
							{{addressForm.address + addressForm.detail_address}}
						</view>
					</view>
				</view>
				<view class="noAddress" v-else>
					<view class="text">
						请选择收获地址
					</view>
					<u-icon name="arrow-right" size="20"></u-icon>
				</view>
			</view>
			<view class="goods">
				<view class="goods-item">
					<view class="img">
						<u--image :showLoading="true" :src="orderForm.product_img" width="180rpx" height="180rpx"></u--image>
					</view>
					<view class="title">
						<view class="name">
							{{orderForm.product_name}}
						</view>
						<view class="num">
							X 1
						</view>
					</view>
					<view class="price">
						¥{{orderForm.product_price}}
					</view>
				</view>
				<view class="message">
					<view class="label">
						留言：
					</view>
					<input type="text" placeholder="留言内容" v-model="form.remark" />
				</view>
				<view class="freight">
					<view class="label">
						运费：
					</view>
					<view class="price">
						¥{{orderForm.freight}}
					</view>
				</view>
				<view class="money">
					<view style="flex: 1;"></view>
					<text class="num">
						共1件
					</text>
					<text class="text">
						小计
					</text>
					<text class="price">
						¥{{orderForm.price}}
					</text>
				</view>
			</view>
		</view>
		<view class="pay-btn">
			<view class="left">
				合计：¥{{orderForm.price}}
			</view>
			<view class="right">
				<view class="btn" @click="payGoods">
					去支付
				</view>
			</view>
		</view>
		<uni-popup ref="popup" type="center">
			<view class="pay-popup">
				<view class="close" @click="close">
					<u-icon name="close" color="#fff" size="28" bold></u-icon>
				</view>
				<view class="successful-img">
					<u--image :showLoading="true" :src="imgPath + 'storage/home/paysuccessful.png'" width="192rpx"
						height="40rpx"></u--image>
				</view>
				<view class="popup-box" :style="{background: `url(${imgPath}storage/home/paybg.png)`,backgroundSize: '100%'}">
					<view class="ok-img">
						<u--image :showLoading="true" :src="imgPath + 'storage/home/payok.png'" width="150rpx"
							height="152rpx"></u--image>
					</view>
					<view class="text">
						您已成功支付，可到个人中心查看！
					</view>
					<view class="btns">
						<view class="left-btn" @click="goHome">
							返回首页
						</view>
						<view class="right-btn" @click="showDetail">
							查看订单
						</view>
					</view>
				</view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	import {
		pay
	} from '@/utils/index.js'
	export default {
		data() {
			return {
				imgPath: this.$store.state.user.baseURL,
				addressForm: {},
				orderForm: {},
				form: {
					product_id: '',
					remark: '',
					address_id: ''
				},
				order_id: ''
			}
		},
		onLoad(e) {
			this.form.product_id = e.id
			this.getAddress()
			this.getProductInfo(e.id)
		},
		methods: {
			getAddress() {
				this.$api.addressinfo({
					is_default: 1
				}).then(res => {
					this.addressForm = res.data
					this.form.address_id = res.data.address_id
				})
			},
			getProductInfo(id) {
				this.$api.productinfo({
					id
				}).then(res => {
					this.orderForm = res.data
				})
			},
			payGoods() {
				if (this.order_id) {
					pay(this.order_id, 3, (() => {
						this.open()
					}))
					return
				}
				this.$api.productOrdercreate(this.form).then(res => {
					this.order_id = res.data.order_id
					pay(res.data.order_id, 3, (() => {
						this.open()
					}))
				})
			},
			goAddress() {
				uni.navigateTo({
					url: '/subpackageB/productdetails/deliveryaddress'
				})
			},
			updataAddress(obj) {
				console.log(obj);
				this.addressForm = obj
				this.form.address_id = obj.address_id
			},
			close() {
				this.$refs.popup.close()
			},
			open() {
				this.$refs.popup.open()
			},
			goHome() {
				this.close()
				uni.switchTab({
					url: '/pages/index/index'
				})
			},
			showDetail() {
				this.close()
				uni.redirectTo({
					url: '/subpackageA/nafuorders/nafuorderdetail?id='+this.order_id
				})
			}
		}
	}
</script>

<style scoped lang="scss">
	.content {
		min-height: 100vh;
		z-index: -1;
	}

	.box {
		box-sizing: border-box;
		padding: 0 30rpx;
		padding-top: 10rpx;
	}

	.address {
		padding: 32rpx 30rpx;
		background: #FEF1DC;
		border-radius: 20rpx;

		.noAddress {
			display: flex;
			justify-content: space-between;

			.text {
				font-family: PingFangSC, PingFang SC;
				font-weight: 500;
				font-size: 28rpx;
				color: #111111;
				line-height: 40rpx;
				text-align: left;
				font-style: normal;
			}
		}

		.haveAddress {
			display: flex;

			.left {
				display: flex;
				align-items: center;
				padding-right: 30rpx;
			}

			.right {
				.top {
					display: flex;

					.name {
						font-family: PingFangSC, PingFang SC;
						font-weight: 500;
						font-size: 32rpx;
						color: #111111;
						line-height: 44rpx;
						text-align: left;
						font-style: normal;
						padding-right: 16rpx;
					}

					.phone {
						font-family: PingFangSC, PingFang SC;
						font-weight: 400;
						font-size: 24rpx;
						color: #999999;
						line-height: 44rpx;
						text-align: left;
						font-style: normal;
					}
				}

				.bottom {
					padding-top: 10rpx;
					font-family: PingFangSC, PingFang SC;
					font-weight: 400;
					font-size: 24rpx;
					color: #999999;
					line-height: 34rpx;
					text-align: left;
					font-style: normal;
				}
			}
		}
	}

	.goods {
		margin-top: 16rpx;
		width: 690rpx;
		height: 564rpx;
		background: #FEF1DC;
		border-radius: 20rpx;
		box-sizing: border-box;
		padding: 35rpx 30rpx;

		.goods-item {
			padding-bottom: 30rpx;
			display: flex;
			border-bottom: 2rpx solid #d4d4d4;

			.img {
				padding-right: 30rpx;
			}

			.title {
				flex: 1;

				.name {
					font-family: PingFangSC, PingFang SC;
					font-weight: 500;
					font-size: 28rpx;
					color: #111111;
					line-height: 40rpx;
					text-align: left;
					font-style: normal;
				}

				.num {
					padding-top: 10rpx;
					font-family: PingFangSC, PingFang SC;
					font-weight: 400;
					font-size: 24rpx;
					color: #AAAAAA;
					line-height: 34rpx;
					text-align: left;
					font-style: normal;
				}
			}

			.price {
				padding-left: 46rpx;
				font-family: PingFangSC, PingFang SC;
				font-weight: 600;
				font-size: 36rpx;
				color: #D06122;
				line-height: 180rpx;
				text-align: left;
				font-style: normal;
			}
		}

		.message {
			display: flex;
			align-items: center;
			padding: 30rpx 0;
			border-bottom: 2rpx solid #d4d4d4;

			.label {
				padding-right: 20rpx;
			}

			input {
				flex: 1;
			}
		}

		.freight {
			display: flex;
			justify-content: space-between;
			padding: 30rpx 0;
			border-bottom: 2rpx solid #d4d4d4;

			.label {
				font-family: PingFangSC, PingFang SC;
				font-weight: 500;
				font-size: 28rpx;
				color: #111111;
				line-height: 40rpx;
				text-align: left;
				font-style: normal;
			}

			.price {
				font-family: PingFangSC, PingFang SC;
				font-weight: 500;
				font-size: 28rpx;
				color: #D06122;
				line-height: 40rpx;
				text-align: left;
				font-style: normal;
			}
		}

		.money {
			padding-top: 30rpx;
			display: flex;

			.num {
				font-family: PingFangSC, PingFang SC;
				font-weight: 400;
				font-size: 28rpx;
				color: #AAAAAA;
				line-height: 40rpx;
				text-align: left;
				font-style: normal;
				padding-right: 10rpx;
			}

			.text {
				font-family: PingFangSC, PingFang SC;
				font-weight: 400;
				font-size: 28rpx;
				color: #111111;
				line-height: 40rpx;
				text-align: left;
				font-style: normal;
				padding-right: 10rpx;
			}

			.price {
				font-family: PingFangSC, PingFang SC;
				font-weight: 500;
				font-size: 28rpx;
				color: #D06122;
				line-height: 40rpx;
				text-align: left;
				font-style: normal;
			}
		}
	}

	.pay-btn {
		position: absolute;
		bottom: 0;
		left: 0;
		width: 100%;
		height: 124rpx;
		background: #FEF1DC;
		display: flex;
		justify-content: space-between;
		align-items: center;
		padding: 0 30rpx;
		box-sizing: border-box;

		.left {
			font-family: PingFangSC, PingFang SC;
			font-weight: 600;
			font-size: 30rpx;
			color: #6A2D0A;
			line-height: 42rpx;
			text-align: left;
			font-style: normal;
		}

		.right {
			.btn {
				width: 220rpx;
				height: 84rpx;
				background: linear-gradient(270deg, #EA6F29 0%, #FAAA7D 100%);
				border-radius: 42rpx;
				font-family: PingFangSC, PingFang SC;
				font-weight: 500;
				font-size: 30rpx;
				color: #FFFFFF;
				line-height: 84rpx;
				text-align: center;
				font-style: normal;
			}
		}
	}

	.pay-popup {
		width: 570rpx;
		height: 636rpx;
		background: linear-gradient(270deg, #FFFFFF 0%, #F2D8AE 100%);
		border-radius: 40rpx;
		box-sizing: border-box;

		.close {
			width: 90rpx;
			height: 90rpx;
			position: absolute;
			bottom: -150rpx;
			left: 50%;
			transform: translateX(-50%);
			border: 8rpx solid white;
			border-radius: 50%;
			display: flex;
			justify-content: center;
			align-items: center;
		}

		.successful-img {
			height: 120rpx;
			display: flex;
			justify-content: center;
			align-items: center;
		}

		.popup-box {
			width: 570rpx;
			height: 516rpx;
			background-size: 100% 100%;
			display: flex;
			flex-direction: column;
			align-items: center;

			.ok-img {
				padding-top: 56rpx;
			}

			.text {
				font-family: PingFangSC, PingFang SC;
				font-weight: 500;
				font-size: 28rpx;
				color: #999999;
				line-height: 40rpx;
				text-align: left;
				font-style: normal;
				padding-top: 44rpx;
			}

			.btns {
				padding-top: 80rpx;
				display: flex;
				justify-content: space-between;
				width: 450rpx;

				.left-btn {
					width: 200rpx;
					height: 84rpx;
					background: #A3A3A3;
					border-radius: 16rpx;
					font-family: PingFangSC, PingFang SC;
					font-weight: 500;
					font-size: 30rpx;
					color: #FFFFFF;
					line-height: 84rpx;
					text-align: center;
					font-style: normal;
				}

				.right-btn {
					width: 200rpx;
					height: 84rpx;
					background: linear-gradient(270deg, #EA6F29 0%, #FAAA7D 100%);
					border-radius: 16rpx;
					font-family: PingFangSC, PingFang SC;
					font-weight: 500;
					font-size: 30rpx;
					color: #FFFFFF;
					line-height: 84rpx;
					text-align: center;
					font-style: normal;
				}
			}
		}
	}
</style>